import requests
import re
import json
from pprint import pprint
from tqdm import tqdm

cookies = {
    '_clck': 'ci9j0i|1|fpb|0',
    'RK': '2N/Nvr/3kP',
    'ptcz': '750f0e5d6f965c12e3cb162572527bafbccf8406a29ab31ff60b0dcb62359abd',
    'pgv_pvid': '5369764833',
    'qq_domain_video_guid_verify': '2b7c4947e5258e96',
    '_qimei_uuid42': '18c120825071004934d4907c00dde7132d489503cc',
    '_qimei_fingerprint': 'd3c60c3b650720acc14bcdd8e80ce840',
    'pgv_info': 'ssid=s7289257825',
    '_qimei_q36': '',
    '_qimei_h38': '2db8ff4234d4907c00dde7130200000c018c12',
    'appuser': 'C4A7B0228F27A6A3',
    'vversion_name': '8.2.95',
    'video_omgid': '2b7c4947e5258e96',
    'o_minduid': 'jcV-E3BFT6qQoEpplRl3JwKpQKt5xEAV',
    'cm_cookie': 'V1,110064&24iNt0qLYNUG&AQEB_LM-A9nYJ8qxFD_47L2LuphqrIXx75QJ&241218&241218',
    'reduplicate_cookie': '110064&24iNt0qLYNUG&1734482478',
    'LZCturn': '290',
    'LPSJturn': '392',
    'LBSturn': '239',
    'LVINturn': '718',
    'LPHLSturn': '118',
    'LDERturn': '967',
    'LZTturn': '306',
    'Lturn': '132',
    'LKBturn': '268',
    'LPVLturn': '98',
    'LPLFturn': '720',
}

headers = {
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-type': 'text/plain;charset=UTF-8',
    # 'cookie': '_clck=ci9j0i|1|fpb|0; RK=2N/Nvr/3kP; ptcz=750f0e5d6f965c12e3cb162572527bafbccf8406a29ab31ff60b0dcb62359abd; pgv_pvid=5369764833; qq_domain_video_guid_verify=2b7c4947e5258e96; _qimei_uuid42=18c120825071004934d4907c00dde7132d489503cc; _qimei_fingerprint=d3c60c3b650720acc14bcdd8e80ce840; pgv_info=ssid=s7289257825; _qimei_q36=; _qimei_h38=2db8ff4234d4907c00dde7130200000c018c12; appuser=C4A7B0228F27A6A3; vversion_name=8.2.95; video_omgid=2b7c4947e5258e96; o_minduid=jcV-E3BFT6qQoEpplRl3JwKpQKt5xEAV; cm_cookie=V1,110064&24iNt0qLYNUG&AQEB_LM-A9nYJ8qxFD_47L2LuphqrIXx75QJ&241218&241218; reduplicate_cookie=110064&24iNt0qLYNUG&1734482478; LZCturn=290; LPSJturn=392; LBSturn=239; LVINturn=718; LPHLSturn=118; LDERturn=967; LZTturn=306; Lturn=132; LKBturn=268; LPVLturn=98; LPLFturn=720',
    'origin': 'https://v.qq.com',
    'priority': 'u=1, i',
    'referer': 'https://v.qq.com/',
    'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
}

data = '{"buid":"vinfoad","vinfoparam":"charge=0&otype=ojson&defnpayver=3&spau=1&spaudio=0&spwm=1&sphls=2&host=v.qq.com&refer=https%3A%2F%2Fv.qq.com%2Fx%2Fcover%2Fmzc002003lw1kp8%2Fc41002vti5z.html&ehost=https%3A%2F%2Fv.qq.com%2Fx%2Fcover%2Fmzc002003lw1kp8%2Fc41002vti5z.html&sphttps=1&encryptVer=9.2&cKey=GA-TmLwt_we1MM1Orq2-LnCjnpb8Ocr0cPTepul8zEul_f4uOWcoVGJNR8G767M9PVFFwh0K3GrqCp7VHCeQghpmp7rG5tiHjLv_PnnatnPaZfOXktuBpd_Iy_Ispo_F0V3i-IIK4cMgFjOj-NmZhE-NjjawCzIdF66cdsFdzz5jk70UOmynTHDptaxqIemxrSlkg-M_BbDaBoWwiX7uSsBkDK_qlv9BvC71CgXl1BaviS_BqOyu7myQxXaZw0af2pySApuO_cBm7THy64l64PYvrkyjWSbCkIPqt090HsInJtXW2dnj58mNnLeut5kJP7AFmOExsITvfbCpmYgjx7BFifu8ZqEYKbfoP7gBj8GpsUQVISZahbkTUidOkxpLSfQlKPdFeKNhm5JmqqxwlaO1dNCR_pe-oj7W56Ihh41ciZHIOlDChqbz4H74huZGce-hoSKacpkEFgo5oDuczhQZo6uO4FoXSoW8G9z4MYDYWrO1lq7RHsg8WDbaDWlRBBWmbfMMmqOUP9BTprb-Jpex5VAEBAQEOR2Dhg&clip=4&guid=2b7c4947e5258e96&flowid=fa3ab4dfe8c392c3a6c78c24ddd82e9a&platform=10201&sdtfrom=v1010&appVer=1.36.3&unid=&auth_from=&auth_ext=&vid=c41002vti5z&defn=&fhdswitch=0&dtype=3&spsrt=2&tm=1734485166&lang_code=0&logintoken=&spvvpay=1&spadseg=3&spvvc=3&spav1=15&hevclv=28&spsfrhdr=0&spvideo=0&spm3u8tag=67&spmasterm3u8=3&track=undefined&drm=40","sspAdParam":"{\\"ad_scene\\":1,\\"pre_ad_params\\":{\\"ad_scene\\":1,\\"user_type\\":0,\\"video\\":{\\"base\\":{\\"vid\\":\\"c41002vti5z\\",\\"cid\\":\\"mzc002003lw1kp8\\"},\\"is_live\\":false,\\"type_id\\":3,\\"referer\\":\\"https://v.qq.com/channel/cartoon\\",\\"url\\":\\"https://v.qq.com/x/cover/mzc002003lw1kp8/c41002vti5z.html\\",\\"flow_id\\":\\"fa3ab4dfe8c392c3a6c78c24ddd82e9a\\",\\"refresh_id\\":\\"fb953831cd8bb1948a7cd3df0cb0ee43_1734482429\\",\\"fmt\\":\\"hd\\"},\\"platform\\":{\\"guid\\":\\"2b7c4947e5258e96\\",\\"channel_id\\":0,\\"site\\":\\"web\\",\\"platform\\":\\"in\\",\\"from\\":0,\\"device\\":\\"pc\\",\\"play_platform\\":10201,\\"pv_tag\\":\\"www_baidu_com|channel\\",\\"support_click_scan_integration\\":true},\\"player\\":{\\"version\\":\\"1.36.3\\",\\"plugin\\":\\"4.1.20\\",\\"switch\\":1,\\"play_type\\":\\"0\\"},\\"token\\":{\\"type\\":0,\\"vuid\\":0,\\"vuser_session\\":\\"\\",\\"app_id\\":\\"\\",\\"open_id\\":\\"\\",\\"access_token\\":\\"\\"},\\"req_extra_info\\":{\\"now_timestamp_s\\":1734485166,\\"ad_frequency_control_time_list\\":{\\"full_pause_feed_back\\":{\\"ad_frequency_control_time_list\\":[1734482537]},\\"full_pause_feedback_successive\\":{\\"ad_frequency_control_time_list\\":[1734482537]}}},\\"extra_info\\":{}}}","adparam":"adType=preAd&vid=c41002vti5z&sspKey=lwoa"}'

if __name__ == "__main__":
    response = requests.post('https://vd6.l.qq.com/proxyhttp', cookies=cookies, headers=headers, data=data)
    data = response.json()
    info_data = data['vinfo']
    result = json.loads(info_data)
    res = result['vl']['vi'][0]['ul']['ui'][-1]['url']
    title = result['vl']['vi'][0]['ti']

    print(title)
    print(res)

    res_url = requests.get(url=res).content
    string_data = res_url.decode('utf-8')

    m3u8 = re.findall(',\n(.*?)\n#',string_data)
    print(m3u8)


    for i in tqdm(m3u8):
        url = ('https://ltscsy.qq.com/B_JxNyiJmktHRgresXhfyMelHqieRSuo61bWduAWuissf7l6wLW-WBSqy_8Y02fLUp/svp_50112'
               '/zUOcxlR5xEO1hNLwwk_pR1-Y7-dA1j6XPSHnzM7NkNgfhAq8AJL6a3fOON_ofWro9ngCNc4FC-Q1mmKT1lSdcsUVGzd3cT-uVucn'
               'KKPahyxtsRQjQ83uOCv_UXdD_NTQLM9MNFp02BK_MXIM_NqtpfE0W8BYr-2TaX3iki_IdKuOiZJXbQoqvU4iPmXV3CgOngMYj9oP3'
               'RzRLx2Bgo5tkFYc1VJirbnW79YJ5yklqQdvNDVNh085uA/') + i
        with open("movie//" + title + ".mp4" ,mode= "ab") as f:
            f.write(requests.get(url).content)
    f.close()
    print(m3u8)